home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / freeWAIS-sf-1.1 / ir / syslog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-19  |  6.2 KB  |  259 lines

  1. /*
  2.  * Copyright (c) 1983, 1988 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. /* Copyright (c) CNIDR (see ../COPYRIGHT) */
  35.  
  36.  
  37. #ifdef NEED_VSYSLOG
  38.  
  39. #if defined(LIBC_SCCS) && !defined(lint)
  40. static char sccsid[] = "@(#)syslog.c    5.34 (Berkeley) 6/26/91";
  41. #endif /* LIBC_SCCS and not lint */
  42.  
  43. #include <sys/types.h>
  44. #include <sys/socket.h>
  45. #include <sys/file.h>
  46. #include <syslog.h>
  47. #include <sys/uio.h>
  48. #include <sys/errno.h>
  49. #include <netdb.h>
  50. /*
  51. #include <string.h>
  52. #ifdef ANSI_LIKE
  53. #include <stdarg.h>
  54. #else
  55. #include <varargs.h>
  56. #endif
  57. #include <time.h>
  58. #include <stdio.h>
  59. */
  60. #include "cdialect.h"
  61.  
  62. static int    LogFile = -1;        /* fd for log */
  63. static int    connected;        /* have done connect */
  64. static int    LogStat = 0;        /* status bits, set by openlog() */
  65. static const char *LogTag = "syslog";    /* string to tag the entry with */
  66. static int    LogFacility = LOG_USER;    /* default facility code */
  67. static int    LogMask = 0xff;        /* mask of priorities to be logged */
  68.  
  69. /*
  70.  * syslog, vsyslog --
  71.  *    print message on log file; output is intended for syslogd(8).
  72.  */
  73. void
  74. #ifdef ANSI_LIKE
  75. syslog(int pri, const char *fmt, ...)
  76. #else
  77. syslog(pri, fmt, va_alist)
  78.     int pri;
  79.     char *fmt;
  80.     va_dcl
  81. #endif
  82. {
  83.     va_list ap;
  84.  
  85. #ifdef ANSI_LIKE
  86.     va_start(ap, fmt);
  87. #else
  88.     va_start(ap);
  89. #endif
  90.     vsyslog(pri, fmt, ap);
  91.     va_end(ap);
  92. }
  93.  
  94. char *strerror(int errno)
  95. {
  96.      extern int sys_nerr;
  97.      extern char *sys_errlist[];
  98.  
  99.      if (errno < sys_nerr) {
  100.       return sys_errlist[errno];
  101.      }
  102.      else {
  103.       return "unknown error";
  104.      }
  105. }
  106.  
  107. void
  108. vsyslog(pri, fmt, ap)
  109.     int pri;
  110.     register const char *fmt;
  111.     va_list ap;
  112. {
  113.     register int cnt;
  114.     register char *p;
  115.     time_t now, time();
  116.     int fd, saved_errno;
  117.     char tbuf[2048], fmt_cpy[1024], *stdp, *ctime();
  118.  
  119.     /* check for invalid bits or no priority set */
  120.     if (!LOG_PRI(pri) || (pri &~ (LOG_PRIMASK|LOG_FACMASK)) ||
  121.         !(LOG_MASK(pri) & LogMask))
  122.         return;
  123.  
  124.     saved_errno = errno;
  125.  
  126.     /* set default facility if none specified */
  127.     if ((pri & LOG_FACMASK) == 0)
  128.         pri |= LogFacility;
  129.  
  130.     /* build the message */
  131.     (void)time(&now);
  132.     (void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4);
  133.     for (p = tbuf; *p; ++p);
  134.     if (LogStat & LOG_PERROR)
  135.         stdp = p;
  136.     if (LogTag) {
  137.         (void)strcpy(p, LogTag);
  138.         for (; *p; ++p);
  139.     }
  140.     if (LogStat & LOG_PID) {
  141.         (void)sprintf(p, "[%d]", getpid());
  142.         for (; *p; ++p);
  143.     }
  144.     if (LogTag) {
  145.         *p++ = ':';
  146.         *p++ = ' ';
  147.     }
  148.  
  149.     /* substitute error message for %m */
  150.     {
  151.         register char ch, *t1, *t2;
  152.         char *strerror();
  153.  
  154.         for (t1 = fmt_cpy; ch = *fmt; ++fmt)
  155.             if (ch == '%' && fmt[1] == 'm') {
  156.                 ++fmt;
  157.                 for (t2 = strerror(saved_errno);
  158.                     *t1 = *t2++; ++t1);
  159.             }
  160.             else
  161.                 *t1++ = ch;
  162.         *t1 = '\0';
  163.     }
  164.  
  165.     (void)vsprintf(p, fmt_cpy, ap);
  166.  
  167.     cnt = strlen(tbuf);
  168.  
  169.     /* output to stderr if requested */
  170.     if (LogStat & LOG_PERROR) {
  171.         struct iovec iov[2];
  172.         register struct iovec *v = iov;
  173.  
  174.         v->iov_base = stdp;
  175.         v->iov_len = cnt - (stdp - tbuf);
  176.         ++v;
  177.         v->iov_base = "\n";
  178.         v->iov_len = 1;
  179.         (void)writev(2, iov, 2);
  180.     }
  181.  
  182.     /* get connected, output the message to the local logger */
  183.     if (!connected)
  184.         openlog(LogTag, LogStat | LOG_NDELAY, 0);
  185.     if (send(LogFile, tbuf, cnt, 0) >= 0)
  186.         return;
  187.  
  188.     /* see if should attempt the console */
  189.     if (!(LogStat&LOG_CONS))
  190.         return;
  191.  
  192.     /*
  193.      * Output the message to the console; don't worry about blocking,
  194.      * if console blocks everything will.  Make sure the error reported
  195.      * is the one from the syslogd failure.
  196.      */
  197.     if ((fd = open("/dev/console", O_WRONLY, 0)) >= 0) {
  198.         (void)strcat(tbuf, "\r\n");
  199.         cnt += 2;
  200.         p = index(tbuf, '>') + 1;
  201.         (void)write(fd, p, cnt - (p - tbuf));
  202.         (void)close(fd);
  203.     }
  204. }
  205.  
  206. static struct sockaddr SyslogAddr;    /* AF_UNIX address of local logger */
  207.  
  208. void
  209. openlog(ident, logstat, logfac)
  210.     const char *ident;
  211.     int logstat, logfac;
  212. {
  213.     if (ident != NULL)
  214.         LogTag = ident;
  215.     LogStat = logstat;
  216.     if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
  217.         LogFacility = logfac;
  218.  
  219.     if (LogFile == -1) {
  220.         SyslogAddr.sa_family = AF_UNIX;
  221.         (void)strncpy(SyslogAddr.sa_data, _PATH_LOG,
  222.             sizeof(SyslogAddr.sa_data));
  223.         if (LogStat & LOG_NDELAY) {
  224.             if ((LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
  225.                 return;
  226.             (void)fcntl(LogFile, F_SETFD, 1);
  227.         }
  228.     }
  229.     if (LogFile != -1 && !connected)
  230.         if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) == -1) {
  231.             (void)close(LogFile);
  232.             LogFile = -1;
  233.         } else
  234.             connected = 1;
  235. }
  236.  
  237. void
  238. closelog()
  239. {
  240.     (void)close(LogFile);
  241.     LogFile = -1;
  242.     connected = 0;
  243. }
  244.  
  245. /* setlogmask -- set the log mask level */
  246. setlogmask(pmask)
  247.     int pmask;
  248. {
  249.     int omask;
  250.  
  251.     omask = LogMask;
  252.     if (pmask != 0)
  253.         LogMask = pmask;
  254.     return (omask);
  255. }
  256.  
  257. #endif /* NEED_VSYSLOG */
  258.  
  259.